/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.anwen.mongo.model.PageResult;
import com.comac.ins.common.core.constant.CommonConstant;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.constant.enums.IsfdAnalysisConfigNodeTypeEnum;
import com.comac.ins.isfd.constant.enums.IsfdAnalysisParameterConfigFieldSourceEnum;
import com.comac.ins.isfd.constant.enums.IsfdParameterConfigTypeEnum;
import com.comac.ins.isfd.domain.*;
import com.comac.ins.isfd.domain.bo.IsfdAnalysisParameterConfigBo;
import com.comac.ins.isfd.domain.bo.query.IsfdAnalysisParameterConfigQueryBo;
import com.comac.ins.isfd.domain.bo.IsfdOperatorParameterConfigBo;
import com.comac.ins.isfd.domain.bo.query.IsfdOperatorParameterConfigQueryBo;
import com.comac.ins.isfd.domain.vo.*;
import com.comac.ins.isfd.service.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 参数库service实现层
 *
 * @author hxloongs
 * @date 2024-7-15
 */
@Slf4j
@Service
public class IsfdParameterConfigServiceImpl implements IIsfdParameterConfigService {

    @Autowired
    private IIsfdAnalysisService iIsfdAnalysisService;

    @Autowired
    private IIsfdOperatorService iIsfdOperatorService;

    @Autowired
    private IIsfdAnalysisConfigService iIsfdAnalysisConfigService;

    @Autowired
    private IIsfdAnalysisParameterConfigService iIsfdAnalysisParameterConfigService;

    @Autowired
    private IIsfdOperatorParameterConfigService iIsfdOperatorParameterConfigService;

    @Override
    public Boolean buildParameterConfigAnalysisTypeAndOperatorName() {
        List<IsfdAnalysisParameterConfig> isfdAnalysisParameterConfigList = iIsfdAnalysisParameterConfigService.list();
        for (IsfdAnalysisParameterConfig isfdAnalysisParameterConfig : isfdAnalysisParameterConfigList) {
            //冗余算子名称
            IsfdOperator operator = iIsfdOperatorService.getById(isfdAnalysisParameterConfig.getOperatorRefId());
            if (operator != null) {
                isfdAnalysisParameterConfig.setOperatorName(operator.getOperatorName());
            }
            //冗余算法名称
            IsfdAnalysis isfdAnalysis = iIsfdAnalysisService.getById(isfdAnalysisParameterConfig.getAnalysisRefId());
            if (isfdAnalysis != null) {
                isfdAnalysisParameterConfig.setAnalysisType(isfdAnalysis.getAnalysisType());
            }
            isfdAnalysisParameterConfig.setCreateTime(new Date());
            iIsfdAnalysisParameterConfigService.saveOrUpdate(isfdAnalysisParameterConfig);
        }
        return Boolean.TRUE;
    }

    @Override
    public List<IsfdParameterConfigCountObjectVo> listCount() {
        // 算法参数总个数
        IsfdParameterConfigCountObjectVo analysisParamConfigVo = new IsfdParameterConfigCountObjectVo();
        List<IsfdAnalysisParameterConfig> isfdAnalysisParameterConfigList = iIsfdAnalysisParameterConfigService.list();
        analysisParamConfigVo.setCount((long) isfdAnalysisParameterConfigList.size());
        analysisParamConfigVo.setName(IsfdParameterConfigTypeEnum.ANALYSIS_CONFIG.getDesc());
        analysisParamConfigVo.setType(IsfdParameterConfigTypeEnum.ANALYSIS_CONFIG.getValue());

        // 算法参数根据 fieldSource 分组，进行统计
        Map<String, Long> isfdAnalysisParameterConfigMap = isfdAnalysisParameterConfigList.stream().collect(Collectors.groupingBy(IsfdAnalysisParameterConfig::getFieldSource, Collectors.counting()));
        List<IsfdParameterConfigCountObjectVo> isfdAnalysisParameterConfigChildren = null;
        if (!CollectionUtil.isEmpty(isfdAnalysisParameterConfigMap)) {
            isfdAnalysisParameterConfigChildren = new ArrayList<>();
            for (String fieldSource : isfdAnalysisParameterConfigMap.keySet()) {
                IsfdParameterConfigCountObjectVo fieldSourceVo = new IsfdParameterConfigCountObjectVo();
                fieldSourceVo.setType(fieldSource);
                fieldSourceVo.setName(fieldSource);
                fieldSourceVo.setCount(isfdAnalysisParameterConfigMap.get(fieldSource));
                isfdAnalysisParameterConfigChildren.add(fieldSourceVo);
            }
            analysisParamConfigVo.setChildren(isfdAnalysisParameterConfigChildren);
        }

        // 算子参数总个数
        IsfdParameterConfigCountObjectVo operatorParamConfigVo = new IsfdParameterConfigCountObjectVo();
        List<IsfdOperatorParameterConfig> isfdOperatorParameterConfigList = iIsfdOperatorParameterConfigService.list();
        operatorParamConfigVo.setCount((long) isfdOperatorParameterConfigList.size());
        operatorParamConfigVo.setName(IsfdParameterConfigTypeEnum.OPERATOR_CONFIG.getDesc());
        operatorParamConfigVo.setType(IsfdParameterConfigTypeEnum.OPERATOR_CONFIG.getValue());

        // 算子参数根据 type 分组，进行统计
        Map<String, Long> isfdOperatorParameterConfigMap = isfdOperatorParameterConfigList.stream().collect(Collectors.groupingBy(IsfdOperatorParameterConfig::getType, Collectors.counting()));
        List<IsfdParameterConfigCountObjectVo> isfdOperatorParameterConfigMapChildren = null;
        if (!CollectionUtil.isEmpty(isfdOperatorParameterConfigMap)) {
            isfdOperatorParameterConfigMapChildren = new ArrayList<>();
            for (String type : isfdOperatorParameterConfigMap.keySet()) {
                IsfdParameterConfigCountObjectVo typeVo = new IsfdParameterConfigCountObjectVo();
                typeVo.setType(type);
                typeVo.setName(type);
                typeVo.setCount(isfdOperatorParameterConfigMap.get(type));
                isfdOperatorParameterConfigMapChildren.add(typeVo);
            }
            operatorParamConfigVo.setChildren(isfdOperatorParameterConfigMapChildren);
        }

        // 封装总返回结果
        List<IsfdParameterConfigCountObjectVo> resultList = new ArrayList<>();
        resultList.add(analysisParamConfigVo);
        resultList.add(operatorParamConfigVo);
        return resultList;
    }

    @Override
    public TableDataInfo<IsfdAnalysisParameterConfigVo> getParameterConfigAnalysisPageList(IsfdAnalysisParameterConfigQueryBo queryBo) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(queryBo.getPageNum());
        pageQuery.setPageSize(queryBo.getPageSize());

        // 如果查询条件中有 analysisRefId，直接按照 analysisRefId 查询
        // 如果没有 analysisRefId，有 operatorName，那么找出所有的 analysisRefId，进行查询
        List<String> analysisRefIdList = null;
        if (StringUtils.isNotBlank(queryBo.getAnalysisRefId())) {
            analysisRefIdList = new ArrayList<>();
            analysisRefIdList.add(queryBo.getAnalysisRefId());
        } else {
            String analysisType = queryBo.getAnalysisType();
            if (StringUtils.isNotBlank(analysisType)) {
                LambdaQueryChainWrapper<IsfdAnalysis> analysisLambdaQueryChainWrapper = iIsfdAnalysisService.lambdaQuery();
                analysisLambdaQueryChainWrapper.like(IsfdAnalysis::getAnalysisType, analysisType);
                List<IsfdAnalysis> analysisList = iIsfdAnalysisService.list(analysisLambdaQueryChainWrapper);
                analysisRefIdList = analysisList.stream().map(IsfdAnalysis::get_id).toList();
            }
        }

        LambdaQueryChainWrapper<IsfdAnalysisParameterConfig> queryChainWrapper = iIsfdAnalysisParameterConfigService.lambdaQuery();
        queryChainWrapper.in(CollectionUtil.isNotEmpty(analysisRefIdList), IsfdAnalysisParameterConfig::getAnalysisRefId, analysisRefIdList);
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldSource, IsfdAnalysisParameterConfigFieldSourceEnum.INPUT.getValue());
        queryChainWrapper.like(StringUtils.isNotBlank(queryBo.getPath()), IsfdAnalysisParameterConfig::getPath, queryBo.getPath());
        queryChainWrapper.like(StringUtils.isNotBlank(queryBo.getLabel()), IsfdAnalysisParameterConfig::getLabel, queryBo.getLabel());
        queryChainWrapper.like(StringUtils.isNotBlank(queryBo.getFieldId()), IsfdAnalysisParameterConfig::getFieldId, queryBo.getFieldId());
        queryChainWrapper.like(StringUtils.isNotBlank(queryBo.getOperatorName()), IsfdAnalysisParameterConfig::getOperatorName, queryBo.getOperatorName());
        queryChainWrapper.in(CollectionUtil.isNotEmpty(queryBo.getMetadataManagementIds()), IsfdAnalysisParameterConfig::getMetadataManagementId, queryBo.getMetadataManagementIds());
        queryChainWrapper.orderByDesc(IsfdAnalysisParameterConfig::getCreateTime);
        queryChainWrapper.orderByAsc(IsfdAnalysisParameterConfig::getAnalysisRefId);


        PageResult<IsfdAnalysisParameterConfig> pageResult = iIsfdAnalysisParameterConfigService.page(queryChainWrapper, pageQuery.getPageNum(), pageQuery.getPageSize());
        long totalSize = pageResult.getTotalSize();
        if (totalSize <= 0) {
            return TableDataInfo.build();
        }
        // 实体类转视图层类
        List<IsfdAnalysisParameterConfigVo> isfdAnalysisParameterConfigVoList = MapstructUtils.convert(pageResult.getContentData(), IsfdAnalysisParameterConfigVo.class);

        return TableDataInfo.buildToPageMemoryLimit(Boolean.FALSE, isfdAnalysisParameterConfigVoList, pageQuery.getPageNum(), pageQuery.getPageSize(), totalSize);
    }

    @Override
    public Boolean parameterConfigAnalysisAdd(IsfdAnalysisParameterConfigBo isfdAnalysisParameterConfigBo) {
        LambdaQueryChainWrapper<IsfdAnalysisParameterConfig> queryChainWrapper = iIsfdAnalysisParameterConfigService.lambdaQuery();
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getAnalysisRefId, isfdAnalysisParameterConfigBo.getAnalysisRefId());
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldSource, IsfdAnalysisParameterConfigFieldSourceEnum.INPUT.getValue());
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getOperatorRefId, isfdAnalysisParameterConfigBo.getOperatorRefId());
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldId, isfdAnalysisParameterConfigBo.getFieldId());
        IsfdAnalysisParameterConfig one = iIsfdAnalysisParameterConfigService.one(queryChainWrapper);
        if (one != null) {
            throw new BaseException("算法层级已经存在该算子的" + isfdAnalysisParameterConfigBo.getFieldId() + "的配置信息！");
        }
        IsfdAnalysisParameterConfig isfdAnalysisParameterConfig = new IsfdAnalysisParameterConfig();
        isfdAnalysisParameterConfig.setAnalysisRefId(isfdAnalysisParameterConfigBo.getAnalysisRefId());
        isfdAnalysisParameterConfig.setOperatorRefId(isfdAnalysisParameterConfigBo.getOperatorRefId());
        isfdAnalysisParameterConfig.setFieldId(isfdAnalysisParameterConfigBo.getFieldId());
        isfdAnalysisParameterConfig.setFieldSource(IsfdAnalysisParameterConfigFieldSourceEnum.INPUT.getValue());
        isfdAnalysisParameterConfig.setPath(isfdAnalysisParameterConfigBo.getPath());
        if (StringUtils.isNotBlank(isfdAnalysisParameterConfigBo.getLabel())) {
            isfdAnalysisParameterConfig.setLabel(isfdAnalysisParameterConfigBo.getLabel());
        }
        //冗余算子名称
        IsfdOperator operator = iIsfdOperatorService.getById(isfdAnalysisParameterConfigBo.getOperatorRefId());
        isfdAnalysisParameterConfig.setOperatorName(operator.getOperatorName());
        //冗余算法名称
        IsfdAnalysis isfdAnalysis = iIsfdAnalysisService.getById(isfdAnalysisParameterConfigBo.getAnalysisRefId());
        isfdAnalysisParameterConfig.setAnalysisType(isfdAnalysis.getAnalysisType());
        isfdAnalysisParameterConfig.setCreateTime(new Date());
        return iIsfdAnalysisParameterConfigService.save(isfdAnalysisParameterConfig);
    }

    @Override
    public Boolean parameterConfigAnalysisUpdate(IsfdAnalysisParameterConfigBo isfdAnalysisParameterConfigBo) {
        IsfdAnalysisParameterConfig isfdAnalysisParameterConfig = iIsfdAnalysisParameterConfigService.getById(isfdAnalysisParameterConfigBo.get_id());
        if (isfdAnalysisParameterConfig == null) {
            throw new BaseException("该算法参数配置不存在，请确认后重试！");
        }
        if (StringUtils.isNotBlank(isfdAnalysisParameterConfigBo.getLabel())) {
            isfdAnalysisParameterConfig.setLabel(isfdAnalysisParameterConfigBo.getLabel());
        }
        isfdAnalysisParameterConfig.setPath(isfdAnalysisParameterConfigBo.getPath());
        return iIsfdAnalysisParameterConfigService.saveOrUpdate(isfdAnalysisParameterConfig);
    }

    @Override
    public Boolean parameterConfigAnalysisRemove(Collection<String> parameterConfigIds) {
        return iIsfdAnalysisParameterConfigService.removeBatchByIds(parameterConfigIds);
    }

    @Override
    public Boolean buildCustomJsonField() {
        List<IsfdOperatorParameterConfig> isfdOperatorParameterConfigList = iIsfdOperatorParameterConfigService.list();
        if (CollectionUtil.isEmpty(isfdOperatorParameterConfigList)) {
            return null;
        }
        AtomicInteger counter = new AtomicInteger(0);
        for (IsfdOperatorParameterConfig isfdOperatorParameterConfig : isfdOperatorParameterConfigList) {
//            String customJsonField = isfdOperatorParameterConfig.getCustomJsonField();
//            if (StringUtils.isNotBlank(customJsonField) && !JSONUtil.parseObj(customJsonField).isEmpty()) {
//                continue;
//            }
            List<IsfdOperatorParameterConfig.Option> options = isfdOperatorParameterConfig.getOptions();
            List<IsfdOperatorParameterConfig.Title> title = isfdOperatorParameterConfig.getTitle();
            List<String> textList = isfdOperatorParameterConfig.getTextList();
            JSONObject jsonObject = new JSONObject();
            if (CollectionUtil.isNotEmpty(options)) {
                jsonObject.set("options", JSONUtil.parseArray(options));
            }
            if (CollectionUtil.isNotEmpty(title)) {
                jsonObject.set("title", JSONUtil.parseArray(title));
            }
            if (CollectionUtil.isNotEmpty(textList)) {
                jsonObject.set("text_list", JSONUtil.parseArray(textList));
            }
            isfdOperatorParameterConfig.setCustomJsonField(JSONUtil.toJsonStr(jsonObject));
//            isfdOperatorParameterConfig.setCreateTime(new Date());

            // 算子名称需要冗余
            IsfdOperator operator = iIsfdOperatorService.getById(isfdOperatorParameterConfig.getOperatorRefId());
            if (operator != null) {
                String operatorName = operator.getOperatorName();
                isfdOperatorParameterConfig.setOperatorName(operatorName);
            }
            log.info("operator is is {}", isfdOperatorParameterConfig.getOperatorRefId());
            iIsfdOperatorParameterConfigService.saveOrUpdate(isfdOperatorParameterConfig);
            counter.incrementAndGet();
        }
        log.info("total build count is {}", counter);
        return Boolean.TRUE;
    }


    @Override
    public TableDataInfo<IsfdOperatorParameterConfigVo> queryPageParameterConfigOperatorList(IsfdOperatorParameterConfigQueryBo queryBo) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(queryBo.getPageNum());
        pageQuery.setPageSize(queryBo.getPageSize());
        // 如果查询条件中有 operatorRefId，直接按照operatorRefId查询
        // 如果没有operatorRefId，有operatorName，那么找出所有的operatorRefId，进行查询
        List<String> operatorRefIdList = null;
        if (StringUtils.isNotBlank(queryBo.getOperatorRefId())) {
            operatorRefIdList = new ArrayList<>();
            operatorRefIdList.add(queryBo.getOperatorRefId());
        } else {
            String operatorName = queryBo.getOperatorName();
            if (StringUtils.isNotBlank(operatorName)) {
                LambdaQueryChainWrapper<IsfdOperator> operatorLambdaQueryChainWrapper = iIsfdOperatorService.lambdaQuery();
                operatorLambdaQueryChainWrapper.like(IsfdOperator::getOperatorName, operatorName);
                List<IsfdOperator> operatorList = iIsfdOperatorService.list(operatorLambdaQueryChainWrapper);
                operatorRefIdList = operatorList.stream().map(IsfdOperator::get_id).toList();
            }
        }
        LambdaQueryChainWrapper<IsfdOperatorParameterConfig> lambdaQueryChainWrapper = iIsfdOperatorParameterConfigService.lambdaQuery();
        lambdaQueryChainWrapper.in(operatorRefIdList != null, IsfdOperatorParameterConfig::getOperatorRefId, operatorRefIdList);
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getComponent()), IsfdOperatorParameterConfig::getComponent, queryBo.getComponent());
        lambdaQueryChainWrapper.eq(StringUtils.isNotBlank(queryBo.getType()), IsfdOperatorParameterConfig::getType, queryBo.getType());
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getPath()), IsfdOperatorParameterConfig::getPath, queryBo.getPath());
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getLabel()), IsfdOperatorParameterConfig::getLabel, queryBo.getLabel());
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getFieldId()), IsfdOperatorParameterConfig::getFieldId, queryBo.getFieldId());
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getFieldType()), IsfdOperatorParameterConfig::getFieldType, queryBo.getFieldType());
        lambdaQueryChainWrapper.eq(StringUtils.isNotBlank(queryBo.getValueType()), IsfdOperatorParameterConfig::getValueType, queryBo.getValueType());
        lambdaQueryChainWrapper.in(CollectionUtil.isNotEmpty(queryBo.getMetadataManagementIds()), IsfdOperatorParameterConfig::getMetadataManagementId, queryBo.getMetadataManagementIds());
        lambdaQueryChainWrapper.orderByDesc(IsfdOperatorParameterConfig::getCreateTime);
        lambdaQueryChainWrapper.orderByAsc(IsfdOperatorParameterConfig::getOperatorRefId);
        lambdaQueryChainWrapper.orderByAsc(IsfdOperatorParameterConfig::getSortOrder);
        PageResult<IsfdOperatorParameterConfig> page = iIsfdOperatorParameterConfigService.page(lambdaQueryChainWrapper, pageQuery.getPageNum(), pageQuery.getPageSize());
        long totalSize = page.getTotalSize();
        if (totalSize <= 0) {
            return TableDataInfo.build();
        }
        List<IsfdOperatorParameterConfigVo> isfdOperatorParameterConfigVoList = MapstructUtils.convert(page.getContentData(), IsfdOperatorParameterConfigVo.class);
        return TableDataInfo.build(isfdOperatorParameterConfigVoList, totalSize);
    }

    @Override
    public IsfdOperatorParameterConfigPathTreeVo queryPageParameterConfigOperatorPathTree() {
        List<IsfdOperatorParameterConfig> isfdOperatorParameterConfigList = iIsfdOperatorParameterConfigService.list();
        if (CollectionUtil.isEmpty(isfdOperatorParameterConfigList)) {
            return null;
        }
        List<String> pathList = isfdOperatorParameterConfigList.stream().map(IsfdOperatorParameterConfig::getPath).toList();
        return IsfdOperatorParameterConfigPathTreeVo.buildTree(pathList);
    }

    @Override
    public Boolean parameterConfigOperatorAdd(IsfdOperatorParameterConfigBo isfdOperatorParameterConfigBo) {
        IsfdOperatorParameterConfig isfdOperatorParameterConfig = MapstructUtils.convert(isfdOperatorParameterConfigBo, IsfdOperatorParameterConfig.class);
        JSONObject customJsonFieldObject = isfdOperatorParameterConfigBo.getCustomJsonFieldObject() != null ? isfdOperatorParameterConfigBo.getCustomJsonFieldObject() : new JSONObject();
        isfdOperatorParameterConfig.setCustomJsonField(JSONUtil.toJsonStr(customJsonFieldObject));
        isfdOperatorParameterConfig.setCreateTime(new Date());
        // 算子名称需要冗余
        if (StringUtils.isBlank(isfdOperatorParameterConfigBo.getOperatorName())) {
            IsfdOperator operator = iIsfdOperatorService.getById(isfdOperatorParameterConfigBo.getOperatorRefId());
            if (operator != null) {
                String operatorName = operator.getOperatorName();
                isfdOperatorParameterConfig.setOperatorName(operatorName);
            }
        }
        return iIsfdOperatorParameterConfigService.save(isfdOperatorParameterConfig);
    }

    @Override
    public Boolean parameterConfigOperatorUpdate(IsfdOperatorParameterConfigBo isfdOperatorParameterConfigBo) {
        IsfdOperatorParameterConfig targetOne = iIsfdOperatorParameterConfigService.getById(isfdOperatorParameterConfigBo.get_id());
        if (targetOne == null) {
            throw new BaseException("该算子参数配置不存在，请确认后重试！");
        }
        IsfdOperatorParameterConfig isfdOperatorParameterConfig = MapstructUtils.convert(isfdOperatorParameterConfigBo, IsfdOperatorParameterConfig.class);
        if (isfdOperatorParameterConfigBo.getCustomJsonFieldObject() != null) {
            isfdOperatorParameterConfig.setCustomJsonField(JSONUtil.toJsonStr(isfdOperatorParameterConfigBo.getCustomJsonFieldObject()));
        }
        // 算子名称需要冗余
        if (StringUtils.isBlank(isfdOperatorParameterConfigBo.getOperatorName())) {
            IsfdOperator operator = iIsfdOperatorService.getById(isfdOperatorParameterConfigBo.getOperatorRefId());
            if (operator != null) {
                String operatorName = operator.getOperatorName();
                isfdOperatorParameterConfig.setOperatorName(operatorName);
            }
        }
        return iIsfdOperatorParameterConfigService.saveOrUpdate(isfdOperatorParameterConfig);
    }

    @Override
    public Boolean parameterConfigOperatorRemove(Collection<String> operatorParameterConfigIds) {
        for (String operatorParameterConfigId : operatorParameterConfigIds) {
            iIsfdOperatorParameterConfigService.removeById(operatorParameterConfigId);
        }
        return Boolean.TRUE;
    }


    @Override
    public List<IsfdOperatorNameVo> getAnalysisOperatorList(String analysisRefId) {
        LambdaQueryChainWrapper<IsfdAnalysisConfig> lambdaQuery = iIsfdAnalysisConfigService.lambdaQuery();
        lambdaQuery.eq(IsfdAnalysisConfig::getAnalysisRefId, analysisRefId);
        lambdaQuery.eq(IsfdAnalysisConfig::getNodeType, IsfdAnalysisConfigNodeTypeEnum.OPERATOR.getValue());
        List<IsfdAnalysisConfig> isfdAnalysisConfigList = iIsfdAnalysisConfigService.list(lambdaQuery);
        if (CollectionUtil.isEmpty(isfdAnalysisConfigList)) {
            throw new BaseException("该算法缺失编排配置！");
        }
        List<String> operatorIds = isfdAnalysisConfigList.stream().map(IsfdAnalysisConfig::getOperatorRefId).toList();
        List<IsfdOperatorNameVo> isfdOperatorNameVoList = new ArrayList<>();
        for (String operatorId : operatorIds) {
            IsfdOperator isfdOperator = iIsfdOperatorService.getById(operatorId);
            IsfdOperatorNameVo isfdOperatorNameVo = new IsfdOperatorNameVo();
            isfdOperatorNameVo.setOperatorName(isfdOperator.getOperatorName());
            isfdOperatorNameVo.setOperatorRefId(operatorId);
            isfdOperatorNameVoList.add(isfdOperatorNameVo);
        }
        return isfdOperatorNameVoList;
    }

    @Override
    public List<IsfdOperatorParameterConfigVo> getFieldIdList(String operatorRefId) {
        LambdaQueryChainWrapper<IsfdOperatorParameterConfig> lambdaQuery = iIsfdOperatorParameterConfigService.lambdaQuery();
        lambdaQuery.eq(IsfdOperatorParameterConfig::getOperatorRefId, operatorRefId);
        List<IsfdOperatorParameterConfig> isfdOperatorParameterConfigList = iIsfdOperatorParameterConfigService.list(lambdaQuery);
        return MapstructUtils.convert(isfdOperatorParameterConfigList, IsfdOperatorParameterConfigVo.class);
    }


    @Override
    public void exportParameterConfigOperator(IsfdOperatorParameterConfigQueryBo queryBo, HttpServletResponse response) {
        // 如果查询条件中有 operatorRefId，直接按照operatorRefId查询
        // 如果没有operatorRefId，有operatorName，那么找出所有的operatorRefId，进行查询
        List<String> operatorRefIdList = null;
        if (StringUtils.isNotBlank(queryBo.getOperatorRefId())) {
            operatorRefIdList = new ArrayList<>();
            operatorRefIdList.add(queryBo.getOperatorRefId());
        } else {
            String operatorName = queryBo.getOperatorName();
            if (StringUtils.isNotBlank(operatorName)) {
                LambdaQueryChainWrapper<IsfdOperator> operatorLambdaQueryChainWrapper = iIsfdOperatorService.lambdaQuery();
                operatorLambdaQueryChainWrapper.like(IsfdOperator::getOperatorName, operatorName);
                List<IsfdOperator> operatorList = iIsfdOperatorService.list(operatorLambdaQueryChainWrapper);
                operatorRefIdList = operatorList.stream().map(IsfdOperator::get_id).toList();
            }
        }
        LambdaQueryChainWrapper<IsfdOperatorParameterConfig> lambdaQueryChainWrapper = iIsfdOperatorParameterConfigService.lambdaQuery();
        lambdaQueryChainWrapper.in(operatorRefIdList != null, IsfdOperatorParameterConfig::getOperatorRefId, operatorRefIdList);
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getComponent()), IsfdOperatorParameterConfig::getComponent, queryBo.getComponent());
        lambdaQueryChainWrapper.eq(StringUtils.isNotBlank(queryBo.getType()), IsfdOperatorParameterConfig::getType, queryBo.getType());
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getPath()), IsfdOperatorParameterConfig::getPath, queryBo.getPath());
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getLabel()), IsfdOperatorParameterConfig::getLabel, queryBo.getLabel());
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getFieldId()), IsfdOperatorParameterConfig::getFieldId, queryBo.getFieldId());
        lambdaQueryChainWrapper.like(StringUtils.isNotBlank(queryBo.getFieldType()), IsfdOperatorParameterConfig::getFieldType, queryBo.getFieldType());
        lambdaQueryChainWrapper.eq(StringUtils.isNotBlank(queryBo.getValueType()), IsfdOperatorParameterConfig::getValueType, queryBo.getValueType());
        lambdaQueryChainWrapper.orderByDesc(IsfdOperatorParameterConfig::getCreateTime);
        lambdaQueryChainWrapper.orderByAsc(IsfdOperatorParameterConfig::getOperatorRefId);
        lambdaQueryChainWrapper.orderByAsc(IsfdOperatorParameterConfig::getSortOrder);
        List<IsfdOperatorParameterConfig> isfdOperatorParameterConfigList = iIsfdOperatorParameterConfigService.list(lambdaQueryChainWrapper);
        if (CollectionUtil.isEmpty(isfdOperatorParameterConfigList)) {
            throw new BaseException("筛选条件为空，请确认筛选条件和结果后再导出");
        }
        List<IsfdOperatorParameterConfigImportVo> resultList = new ArrayList<>();
        for (IsfdOperatorParameterConfig isfdOperatorParameterConfig : isfdOperatorParameterConfigList) {
            IsfdOperatorParameterConfigImportVo isfdOperatorParameterConfigImportVo = new IsfdOperatorParameterConfigImportVo();
            isfdOperatorParameterConfigImportVo.setOperatorName(isfdOperatorParameterConfig.getOperatorName());
            isfdOperatorParameterConfigImportVo.setType(isfdOperatorParameterConfig.getType());
            isfdOperatorParameterConfigImportVo.setPath(isfdOperatorParameterConfig.getPath());
            isfdOperatorParameterConfigImportVo.setSortOrder(isfdOperatorParameterConfig.getSortOrder());
            isfdOperatorParameterConfigImportVo.setComponent(isfdOperatorParameterConfig.getComponent());
            isfdOperatorParameterConfigImportVo.setLabel(isfdOperatorParameterConfig.getLabel());
            isfdOperatorParameterConfigImportVo.setFieldId(isfdOperatorParameterConfig.getFieldId());
            isfdOperatorParameterConfigImportVo.setFieldType(isfdOperatorParameterConfig.getFieldType());
            isfdOperatorParameterConfigImportVo.setCustomJsonField(isfdOperatorParameterConfig.getCustomJsonField());
            resultList.add(isfdOperatorParameterConfigImportVo);
        }
        ExcelUtil.exportTemplateWithDataPointSheetIndex(resultList, 0, CommonConstant.OPERATOR_PARAMETER_CONFIG_NAME, CommonConstant.OPERATOR_PARAMETER_CONFIG_FILE_PATH, response);
    }

    @Override
    public IsfdOperatorParameterConfigVo one(String operatorRefId, String path, String type, String fieldId, String fieldType) {
        LambdaQueryChainWrapper<IsfdOperatorParameterConfig> lambdaQuery = iIsfdOperatorParameterConfigService.lambdaQuery();
        lambdaQuery.eq(IsfdOperatorParameterConfig::getOperatorRefId, operatorRefId);
        lambdaQuery.eq(IsfdOperatorParameterConfig::getPath, path);
        lambdaQuery.eq(IsfdOperatorParameterConfig::getType, type);
        lambdaQuery.eq(IsfdOperatorParameterConfig::getFieldId, fieldId);
        lambdaQuery.eq(IsfdOperatorParameterConfig::getFieldType, fieldType);
        IsfdOperatorParameterConfig isfdOperatorParameterConfig = iIsfdOperatorParameterConfigService.one(lambdaQuery);
        if (isfdOperatorParameterConfig == null) {
            return null;
        }
        return MapstructUtils.convert(isfdOperatorParameterConfig, IsfdOperatorParameterConfigVo.class);
    }

    @Override
    public TableDataInfo<IsfdOperatorMapParameterVo> getConfigRelatedOperatorMap(String operatorRefId, String analysisRefId, PageQuery pageQuery) {
        List<IsfdOperatorMapParameterVo> resultList = new ArrayList<>();

        LambdaQueryChainWrapper<IsfdAnalysisParameterConfig> queryChainWrapper = iIsfdAnalysisParameterConfigService.lambdaQuery();
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getAnalysisRefId, analysisRefId);
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getOperatorRefId, operatorRefId);
        queryChainWrapper.eq(IsfdAnalysisParameterConfig::getFieldSource, IsfdAnalysisParameterConfigFieldSourceEnum.OPERATOR_RESULT.getValue());
        // 查询数据库若已经做过参数映射则直接返回信息
        PageResult<IsfdAnalysisParameterConfig> isfdAnalysisParameterConfigPageResult = iIsfdAnalysisParameterConfigService.page(queryChainWrapper, pageQuery.getPageNum(), pageQuery.getPageSize());
        List<IsfdAnalysisParameterConfig> isfdAnalysisParameterConfigList = isfdAnalysisParameterConfigPageResult.getContentData();
        // 结果如果是空，直接返回
        if (CollectionUtil.isEmpty(isfdAnalysisParameterConfigList)) {
            return TableDataInfo.build();
        }

        for (IsfdAnalysisParameterConfig isfdAnalysisParameterConfig : isfdAnalysisParameterConfigList) {
            String relatedOperatorRefId = isfdAnalysisParameterConfig.getRelatedOperatorRefId();
            // 获取父节点算子的name
            IsfdOperatorMapParameterVo isfdOperatorMapParameterVo = new IsfdOperatorMapParameterVo();
            if(StringUtils.isNotBlank(relatedOperatorRefId)){
                IsfdOperator isfdOperator = iIsfdOperatorService.getById(relatedOperatorRefId);
                isfdOperatorMapParameterVo.setRelatedOperatorName(isfdOperator.getOperatorName());
            }
            // 更新返回结果列表
            isfdOperatorMapParameterVo.setFieldId(isfdAnalysisParameterConfig.getFieldId());
            isfdOperatorMapParameterVo.setRelatedFieldId(isfdAnalysisParameterConfig.getRelatedFieldId());
            isfdOperatorMapParameterVo.setRelatedOperatorRefId(relatedOperatorRefId);
            resultList.add(isfdOperatorMapParameterVo);
        }
        return TableDataInfo.buildToPageMemoryLimit(Boolean.FALSE, resultList, pageQuery.getPageNum(), pageQuery.getPageSize(),
            isfdAnalysisParameterConfigPageResult.getTotalSize());
    }
}
